/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-04-16
 * Time:20:05
 */

/**
 * Jz4二维数组中的查找
 */

public class Find {
    public static void main(String[] args) {
//        int[][] array = new int[][] {
//                {1,2,8,9},
//                {2,4,9,12},
//                {4,7,10,13},
//                {6,8,11,15}
//        };
//        Solution solution = new Solution();
//        int target1 = 7;
//        int target2 = 3;
//        System.out.println(Find(target1,array)); // true
//        System.out.println(Find(target2,array)); // false
        int[][] matrix = new int[][]{
                {1, 2, 8, 9},
                {2, 4, 9, 12},
                {4, 7, 10, 13},
                {6, 8, 11, 15}
        };
        int target = 15;
        System.out.println(Find(target,matrix));
    }
    public static boolean Find(int target, int [][] array) {
        int lenR = array.length;
        int lenC = array[0].length;
        int indexR = 0;
        int indexC = lenC;
        while (indexR < lenR && indexC > 0){
            if (array[indexR][indexC - 1] == target){
                return true;
            }else {
                if (target > array[indexR][indexC - 1]){
                    indexR++;
                    indexC = lenC;
                }else {
                    indexC--;
                }
            }
        }
        return false;
    }
}
